package com.yj.oa.service;

import com.yj.oa.dto.system.OABindingDto;
import com.yj.oa.entity.oa.User;
import com.yj.oa.entity.system.OAUserConfig;
import com.yj.oa.entity.system.OAUserInfo;
import com.yj.oa.entity.system.UserRelationship;
import com.yj.oa.entity.system.YMWhitelist;
import com.yj.oa.mapper.OAUserConfigMapper;
import com.yj.oa.mapper.OAUserInfoMapper;
import com.yj.oa.mapper.UserRelationshipMapper;
import com.yj.oa.mapper.YMWhitelistMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Copyright 2017 (C) Yunjian-VC
 * Created on : 2017/12/1
 * Author     : Unbelievable9
 **/
@Service
public class OABindingService {

    @Autowired
    private YMWhitelistMapper ymWhitelistMapper;

    @Autowired
    private UserRelationshipMapper userRelationshipMapper;

    @Autowired
    private OAUserInfoMapper oaUserInfoMapper;

    @Autowired
    private OAUserConfigMapper oaUserConfigMapper;

    public Boolean whiteListCheck(String phone) {
        YMWhitelist whitelist = ymWhitelistMapper.getByPhone(phone);

        return whitelist != null;
    }

    public UserRelationship oaCheck(String oa_username) {
        UserRelationship userRelationship = userRelationshipMapper.getByOAUsername(oa_username);

        if (userRelationship != null) {
            return userRelationship;
        } else {
            return null;
        }
    }

    public OAUserInfo bindingCheck(Integer ym_user_id) {
        UserRelationship userRelationship = userRelationshipMapper.getByYmUserID(ym_user_id);

        if (userRelationship != null) {
            return oaUserInfoMapper.getOAUserInfoByUserId(userRelationship.getOa_user_id());
        } else {
            return null;
        }
    }

    public void createNewRelationship(OABindingDto oaBindingDto, User user) {
        UserRelationship userRelationship = new UserRelationship();
        userRelationship.setYm_user_id(oaBindingDto.getYm_user_id());
        userRelationship.setYm_user_phone(oaBindingDto.getYm_user_phone());
        userRelationship.setOa_user_id(user.getUser_id());
        userRelationship.setOa_username(oaBindingDto.getOa_username());
        userRelationship.setOa_user_token(user.getToken());

        userRelationshipMapper.insert(userRelationship);
    }

    public void createNewOAUserInfo(OAUserInfo oaUserInfo) {
        OAUserConfig oaUserConfig = oaUserConfigMapper.getByUserID(oaUserInfo.getUser_id());

        oaUserInfo.setPhone(oaUserConfig.getCellphone());

        oaUserInfoMapper.insert(oaUserInfo);
    }
}
